#!/usr/bin/env bash

[ ! -d /data ] && mkdir /data

read -p "Enter an instance id(such as port number 3306): " INSTANCE

if [ -d /data/$INSTANCE ]; then
    echo "$INSTANCE is already exists, try another..."
    exit 1
else
    mkdir -p /data/$INSTANCE/data
fi

/bin/cp /data/3306/my.cnf /data/$INSTANCE
/bin/cp /data/3306/mysql /data/$INSTANCE

sed -i 's#3306#3309#g' /data/$INSTANCE/my.cnf
sed -i 's#server-id=1#server-id=9#g' /data/$INSTANCE/my.cnf
sed -i 's#3306#3309#g' /data/$INSTANCE/mysql

cd /application/mysql/scripts
./mysql_install_db --datadir=/data/$INSTANCE/data --basedir=/application/mysql --user=mysql

chown -R mysql.mysql /data/$INSTANCE
/data/$INSTANCE/mysql start

sleep 5

mysqladmin -S /data/$INSTANCE/mysql.sock password 123456

netstat -antup | grep 3309
